ARD2  RC2
Airbag Reference Demonstrator using MPC5604P
GUI_State.c
Go to the documentation of this file.
00001 
00016 #include "derivative.h"
00017 #include "Compile_Options.h"
00018 #include "GUI_State.h"
00019 #include "SM.h"
00020 #include "SBC_AL.h"
00021 #include "CG147.h"
00022 #include "CG147_Diag.h"
00023 #include "Central_Accel_AL.h"
00024 #include "MMA68xx.h"
00025 #include "MMA68xx_Diag.h"
00026 #include "DSPI.h"
00027 #include "HAL.h"
00028 #include "MailScheduler.h"
00029 #include "Application_Globals.h"
00030 #include "MMA51xx.h"
00031 #include "SIU.h"
00032 #include "Labview.h"
00033 #include "IntcInterrupts.h"
00034 #include "Utils.h"
00035 #include "LIN_UART.h"
00036 #include <Limits.h>
00037 /*
00038  ******************************************************************************
00039  * Constants
00040  ******************************************************************************
00041  */
00043 static const uint16_t cau16PreGUIValidStates[] =
00044 {
00045   SM_STATE_ERROR, SM_STATE_DEPLOYMENT };
00046 
00047 static const uint8_t cau8SampleTable[] = 
00048 {
00049   1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17, 16, 18, 19, 20,
00050   21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40
00051 };
00053 static const uint8_t cau8GUIStat00[] = "Please wait while the command is being executed.";
00055 static const uint8_t cau8GUIStat01[] = "The command was not recognized.";
00057 static const uint8_t cau8GUIStat02[] = "The command has been executed.";
00058 static const uint8_t cau8GUIStat03[] = "Active squibs: ";
00059 static const uint8_t cau8GUIStat04[] = "Active satellites: ";
00063 static const GUICmdString_t catGUIValidCommands[] = 
00064 {
00065   "3**", "a**", "c**", "e0*", "e1*", "g0*", "g1*", "i0*", "i1*", "k**", "m**", 
00066   "o**", "q**", "s**", "C**"
00067 };
00068 /*
00069  ******************************************************************************
00070  * State Globals
00071  ******************************************************************************
00072  */
00074 static uint8_t gu8GUIStateStepCounter;
00075 /*
00076  ******************************************************************************
00077  * u32fnStateGUI
00078  ******************************************************************************
00079  */
00080 uint32_t u32fnStateGUI(void)
00081 {
00082 #ifdef USE_LABVIEW  
00083   uint16_t au16LabviewData[8u];
00084   uint8_t au16LabviewRawResponse[15];
00085   uint8_t au8TextResponse[256];
00086   uint8_t u8TextSize;
00087 #endif
00088   uint32_t u32Status;
00089   uint32_t u32GUITime;
00090   uint8_t  u8ResponseSize;
00091   
00092 
00093   
00094   /* Verify that we can execute - Previous state ended correctly */
00095   u32Status = u32fnSMValidateCurrentState((uint16_t*)cau16PreGUIValidStates,
00096                                           N_ELEMENTS(cau16PreGUIValidStates));
00097   if(CLEAR == u32Status)
00098   {
00099     /* Here comes the real contents of the state */
00100     LOCK_STATE_EXECUTION();
00101 
00102 #ifdef USE_LABVIEW  
00103     if(gu8GUIStateFirstRun == GUI_STATE_RUN_HAS_BEEN_EXECUTED)
00104     {
00105       /* Enable communication with the PC */
00106       vfnLVEnable(TRUE);
00107     }
00108     else
00109     {
00110       /* Remember we've been through this */
00111       gu8GUIStateFirstRun = GUI_STATE_RUN_HAS_BEEN_EXECUTED;
00112       gu8GUIStateStepCounter = CLEAR;
00113       gau8LabViewRxCmd[CLEAR] = CLEAR;
00114       
00115       /* Configure our device to receive data */
00116       if(CLEAR == u8fnLVConfigRxCmd())
00117       {
00118         /* Go on */
00119       }
00120       else
00121       {
00122         /* We should flag it as an error, but we don't really care because */
00123         /* this is just a demo. Instead, we will reset gu8GUIStateFirstRun */
00124         /* in order to do this again */
00125         gu8GUIStateFirstRun = CLEAR;
00126       }
00127     }
00128     
00129     /* ********* True behavior of the state starts here **********************/
00130     /* Assign the time */
00131     SCHED_WHAT_IS_THE_TIME(u32GUITime);
00132     
00133     /* Now we wait. We shall not pass more time here than we should */
00134     do
00135     {
00136       if(CLEAR == u8fnLVReceiveCmd())
00137       {
00138         /* We have a request for an action. Treat it. Execution of the         */
00139         /* regular state-machine, including timing, etc, goes to the way-side  */
00140         /* But first, inform the user we have gotten the command. */
00141         vfnCopyArray((uint8_t*)&cau8GUIStat00, (uint8_t*)au8TextResponse, \
00142                      N_ELEMENTS(cau8GUIStat00));
00143         /* Allow any pending message to finish */
00144         (void)u8fnLVWaitForTxPort();
00145         u32Status = u8fnLabViewSendMSorMP((uint8_t*)au8TextResponse, 
00146                                           N_ELEMENTS(cau8GUIStat00),
00147                                           LV_SEND_SERIALLY, LV_TS_MSG);
00148         
00149         u32Status = u32fnGUISwitchCase((uint8_t*)&gau8LabViewRxCmd[LV_CMD],
00150                                        (uint8_t*)au16LabviewRawResponse,
00151                                        (uint8_t*)&u8ResponseSize,
00152                                        (uint8_t*)&au8TextResponse,
00153                                        (uint8_t*)&u8TextSize);
00154         u32Status = u32fnGUISendResponseFrame((const uint8_t*)&gau8LabViewRxCmd[LV_CMD],
00155                                               (const uint8_t*)au16LabviewRawResponse,
00156                                               (const uint8_t*)&u8ResponseSize,
00157                                               (const uint8_t*)au8TextResponse,
00158                                               u8TextSize);
00159         
00160         gau8LabViewRxCmd[CLEAR] = CLEAR;
00161         
00162 #ifndef USE_AUTO_SYNC
00163         /* Ensure that we have data in our satellites when we exit this fn */
00164         (void)u8fnSBCSchedulePSync();
00165         DELAY_MSEC(1u);
00166 #endif
00167         
00168       }
00169       else
00170       {
00171         /* Let's tell our GUI what the status is of our satellites + squibs */
00172         vfnGUISatAndSquibState(LV_SEND_IN_PARALLEL, gu8GUIStateStepCounter++);
00173         if((CLEAR == gau8LINUARTWordsTx[LABVIEW_SCI]) & (gu8GUIStateStepCounter > 14))
00174         {
00175           vfnGUIStateFormatCurveData((uint16_t*)&au16LabviewData);
00176           vfnDisplaySCIDataParallel(LV_DA_MSG, LV_16, 6u, 1u,
00177                                     (uint8_t*)au16LabviewData);
00178           if(0x100u > gu8GUIStateStepCounter)
00179           {
00180             /* Keep counting */
00181           }
00182           else
00183           {
00184             gu8GUIStateStepCounter = CLEAR;
00185           }
00186           
00187         }
00188         else
00189         {
00190           /* Nothing */
00191          
00192         }
00193       }
00194     }while(SCHED_100US_PERIOD  > u32fnSchedHasTimeElapsed(u32GUITime));
00195     
00196     /* Disable communication with the PC */
00197     vfnLVEnable(CLEAR);
00198 #endif /* Use Labview */    
00199     /* Unlock the state */
00200     UNLOCK_STATE_EXECUTION();
00201   }
00202   else
00203   {
00204     /* Leave */
00205   }
00206 
00207   /* Set the next state */
00208   if(CLEAR == u32Status)
00209   {
00210     vfnSMWriteNextState(SM_APPLICATION_START);
00211   }
00212   else
00213   {
00214     vfnSMWriteNextState(SM_STATE_ERROR);
00215   }
00216   return (u32Status);
00217 }
00218 /*
00219  ******************************************************************************
00220  * vfnGUIStateFormatCurveData
00221  ******************************************************************************
00222  */
00223 void vfnGUIStateFormatCurveData(uint16_t* au16LabviewData)
00224 {
00225   /* Data manipulation occurs here */
00226   if(BIT0 & gu16ActivePSI5Channels)
00227   {
00228     au16LabviewData[0u] = gau16RawAccels[0u];
00229   }
00230   else
00231   {
00232     au16LabviewData[0u] = 512;
00233   }
00234   if(BIT3 & gu16ActivePSI5Channels)
00235   {
00236     au16LabviewData[1u] = gau16RawAccels[3u];
00237   }
00238   else
00239   {
00240     au16LabviewData[1u] = 512;
00241   }
00242   if(BIT6 & gu16ActivePSI5Channels)
00243   {
00244     au16LabviewData[2u] = gau16RawAccels[6u];
00245   }
00246   else
00247   {
00248     au16LabviewData[2u] = 512;
00249   }
00250   if(BIT9 & gu16ActivePSI5Channels)
00251   {
00252     au16LabviewData[3u] = gau16RawAccels[9u];
00253   }
00254   else
00255   {
00256     au16LabviewData[3u] = 512;
00257   }
00258   au16LabviewData[4u] = gau16RawAccels[12u];
00259   au16LabviewData[5u] = gau16RawAccels[13u];
00260   return;
00261 }  
00262 /*
00263  ******************************************************************************
00264  * u32fnGUISwitchCase
00265  ******************************************************************************
00266  */
00267 uint32_t u32fnGUISwitchCase(const uint8_t* pu8Cmd, 
00268                             uint8_t* pu8RawResponse, 
00269                             uint8_t* pu8Size,
00270                             uint8_t* pu8TextResponse,
00271                             uint8_t* pu8TextSize)
00272 {
00273   uint32_t u32Status;
00274   uint8_t  au8ScratchPad[4u];
00275   uint32_t u32ScratchPad;
00276   uint8_t  au8Args[N_GUI_ARGS];
00277   uint8_t  u8Counter;
00278   u32Status = CLEAR;
00279   
00280   /* Decode the ASCII elements of the arguments and place them as Hex */
00281   vfnASCIIByteToHex((uint8_t*)(pu8Cmd + (uint8_t)1u), (uint8_t*)&au8Args, N_GUI_ARGS);
00282   
00283   /* Raw response shall contain the Pass/Fail (TRUE/CLEAR) result as the */
00284   /* first element of the RawResponse; following elements are treated on */
00285   /* a case-by-case scenario. */
00286   
00287   /* We will assume that the command received is good, unless shown otherwise */
00288   vfnCopyArray((uint8_t*)&cau8GUIStat02, pu8TextResponse, \
00289                                   N_ELEMENTS(cau8GUIStat02));
00290   *pu8TextSize = N_ELEMENTS(cau8GUIStat02);
00291   
00292   DELAY_MSEC(1u);
00293   
00294   switch(*pu8Cmd)
00295   {
00296     case ('1'):
00297     {
00298       /* In this case, we must figure out what elements in a list need to be */
00299       /* active.                                                             */
00300       vfnVerifyValidityOfCmd(pu8Cmd, 
00301                              (const GUICmdString_t*)&catGUIValidCommands,
00302                               N_ELEMENTS(catGUIValidCommands),
00303                               pu8Size);
00304       *pu8RawResponse = *pu8Size;
00305       break;
00306     }
00307     case ('3'):
00308     {
00309       /* Read sample registers */
00310       vfnDisplaySCIRegistersInTable(LV_RS_MSG, 
00311                                     CLEAR, 
00312                                     40, 
00313                                     (uint8_t*)&cau8SampleTable);
00314       break;
00315     }
00316     case ('a'):
00317     {
00318       /* Cap test */
00319       
00320       vfnGUISBCCapTests(pu8Size, pu8RawResponse,
00321                         au8Args[CLEAR], CLEAR);
00322       break;
00323     }
00324     case ('c'):
00325     {
00326       /* ESR test */
00327       vfnGUISBCCapTests(pu8Size, pu8RawResponse,
00328                         au8Args[CLEAR], (uint8_t)1u);
00329       
00330       break;
00331     }
00332     case ('e'):
00333     {
00334       /* Polarity protection check */
00335       vfnGUISBCCapTests(pu8Size, pu8RawResponse,
00336                         au8Args[CLEAR], (uint8_t)2u);
00337       break;
00338     }
00339     case ('g'):
00340     {
00341       /* Low leakage test */
00342       vfnGUISBCLeakageTests(au8Args[CLEAR],
00343                             au8Args[TRUE], 
00344                             TRUE, 
00345                             pu8RawResponse);
00346       *pu8Size = 7u;
00347       break;
00348     }
00349     case ('i'):
00350     {
00351       /* High leakage test */
00352       vfnGUISBCLeakageTests(au8Args[CLEAR],
00353                             au8Args[TRUE], 
00354                             CLEAR, 
00355                             pu8RawResponse);
00356       *pu8Size = 7u;
00357       break;
00358     }
00359     case ('k'):
00360     {
00361       /* Resistance test */
00362       u32ScratchPad = 
00363         u32fnSBCFLMResistanceMeasurement(cau8SBCInitialTestAoutRoutesIGH[au8Args[TRUE]],
00364                                          cau8SBCInitialTestAoutRoutesIGL[au8Args[TRUE]],
00365                                          au8Args[TRUE]);
00366      *pu8RawResponse =  (uint8_t)(CLEAR == u32ScratchPad);
00367      *(pu8RawResponse + 1u) = (uint8_t)(u32ScratchPad >> BITS_IN_24);
00368      *(pu8RawResponse + 2u) = (uint8_t)(u32ScratchPad >> BITS_IN_16);
00369      *(pu8RawResponse + 3u) = (uint8_t)(u32ScratchPad >> BITS_IN_BYTE);
00370      *(pu8RawResponse + 4u) = (uint8_t)u32ScratchPad;
00371      *pu8Size = 5u;
00372       break;
00373     }
00374     case ('m'):
00375     {
00376       /* Mux test */
00377       u32ScratchPad = CLEAR;
00378       for(u8Counter = CLEAR; u8Counter < 5u; u8Counter++)
00379       {
00380         u32ScratchPad |= u32fnSBCTestMuxAndADC(u8Counter);
00381         DELAY_MSEC(10u);
00382       }
00383       *pu8RawResponse =  (uint8_t)(CLEAR == u32ScratchPad);
00384       *(pu8RawResponse + 1u) = (uint8_t)(u32ScratchPad >> BITS_IN_24);
00385       *(pu8RawResponse + 2u) = (uint8_t)(u32ScratchPad >> BITS_IN_16);
00386       *(pu8RawResponse + 3u) = (uint8_t)(u32ScratchPad >> BITS_IN_BYTE);
00387       *(pu8RawResponse + 4u) = (uint8_t)u32ScratchPad;
00388       *pu8Size = 5u;
00389       break;
00390     }
00391     case ('o'):
00392     {
00393       /* CG147's ADC test */
00394       u32ScratchPad = CLEAR;
00395       for(u8Counter = 5u; u8Counter < cu8SizeOfTestAnaHeadSettings; u8Counter++)
00396       {
00397         u32ScratchPad |= u32fnSBCTestMuxAndADC(u8Counter);
00398         DELAY_MSEC(10u);
00399       }
00400       *pu8RawResponse =  (uint8_t)(CLEAR == u32ScratchPad);
00401       *(pu8RawResponse + 1u) = (uint8_t)(u32ScratchPad >> BITS_IN_24);
00402       *(pu8RawResponse + 2u) = (uint8_t)(u32ScratchPad >> BITS_IN_16);
00403       *(pu8RawResponse + 3u) = (uint8_t)(u32ScratchPad >> BITS_IN_BYTE);
00404       *(pu8RawResponse + 4u) = (uint8_t)u32ScratchPad;
00405       *pu8Size = 5u;
00406       break;
00407     }
00408     case('q'):
00409     {
00410       /* Select the channel we want to measure */
00411       au8ScratchPad[CLEAR] = (uint8_t)((au8Args[CLEAR] & 0x0F) << BITS_IN_NIBBLE);
00412       au8ScratchPad[CLEAR] |= (BIT7 | (uint8_t)au8Args[TRUE]); 
00413       u32ScratchPad = u8fnCG147ScheduleSafeTransfer(CG147_SPI_CONFIG_EXT,
00414                                                     SBC_AOUT_CTRL,
00415                                                     au8ScratchPad[CLEAR],
00416                                                     (uint16_t*)&au8ScratchPad, 
00417                                                     (uint32_t*)&au8ScratchPad);
00418       /* Wait for the level to take */
00419       DELAY_MSEC(1u);
00420       
00421       /* Read it back */
00422       /* Store the current Voltage level in Aout. This is our reference point. */
00423       vfnTransferADCResults(CLEAR, (uint16_t*)&au8ScratchPad, 2u);
00424       
00425       /* Wait just to be sure */
00426       DELAY_MSEC(2u);
00427       
00428       /* Start constructing result */
00429       *pu8RawResponse =  (uint8_t)(TRUE);
00430       *(pu8RawResponse + 1u) = au8ScratchPad[CLEAR];
00431       *(pu8RawResponse + 2u) = au8ScratchPad[1];
00432       *(pu8RawResponse + 3u) = au8ScratchPad[2];
00433       *(pu8RawResponse + 4u) = au8ScratchPad[3];
00434       
00435       *pu8Size = 3u;
00436       break;
00437     }
00438     case('s'):
00439     {
00440       /* Check satellites */
00441       au8ScratchPad[CLEAR] = CLEAR;
00442       vfnGUICheckSatellites((uint16_t*)&au8ScratchPad, pu8RawResponse);
00443       *pu8Size = (uint8_t)1u;
00444       break;
00445     }
00446     case ('A'):
00447     {
00448       /* Cap test */
00449       break;
00450     }
00451     case ('C'):
00452     {
00453       /* Central Accelerometer Self test */
00454       /* Reset to allow future re-programming */
00455       u32Status = (*u8pfnMMA6800BatchOp[MMA6800_TRANSFER_MODE_IS_SCHEDULED])
00456                 (MAIN_ACCELERO_SPI_CONFIG,(uint16_t*)cau16MMA6800ResetSettings,
00457                 (uint16_t*)gau8MMA6800GlobalResult,
00458                 cu8SizeofMMA6800ResetSettings);        
00459       DELAY_MSEC(1u);
00460       /* Load init settings */      
00461       u32Status = (*u8pfnMMA6800BatchOp[MMA6800_TRANSFER_MODE_IS_SCHEDULED])
00462                 (MAIN_ACCELERO_SPI_CONFIG,(uint16_t*)cau16MMA6800InitSettings,
00463                 (uint16_t*)gau8MMA6800GlobalResult,
00464                 cu8SizeofMMA6800InitSettings);       
00465       DELAY_MSEC(1u);
00466       if (CLEAR == au8Args[CLEAR])
00467       {
00468          /* CA Self test for both axis */      
00469          u32ScratchPad = u32fnCAInitSelfTest();
00470       }
00471       else
00472       {
00473         if (CLEAR == au8Args[1])
00474         {
00475           /* CA Self test for X axis */      
00476           u32ScratchPad = u32fnCASelfTest((uint8_t) MMA6800_READ_X);          
00477         }
00478         else
00479         {
00480           /* CA Self test for Y axis */      
00481           u32ScratchPad = u32fnCASelfTest((uint8_t) MMA6800_READ_Y);          
00482         }
00483       }
00484       /* ENDINIT bit set */
00485       DELAY_MSEC(1u);      
00486       u32Status = u8fnMMA6800WriteRegister(MAIN_ACCELERO_SPI_CONFIG,
00487                                            MMA6800_TRANSFER_MODE_IS_SCHEDULED,
00488                                            MMA6800_DEVCFG, 0x35, 
00489                                            (uint16_t*) &gau8MMA6800GlobalResult);
00490       
00491      *pu8RawResponse =  (uint8_t)(CLEAR == u32ScratchPad);
00492      *(pu8RawResponse + 1u) = (uint8_t)(u32ScratchPad >> BITS_IN_24);
00493      *(pu8RawResponse + 2u) = (uint8_t)(u32ScratchPad >> BITS_IN_16);
00494      *(pu8RawResponse + 3u) = (uint8_t)(u32ScratchPad >> BITS_IN_BYTE);
00495      *(pu8RawResponse + 4u) = (uint8_t)u32ScratchPad;
00496      *pu8Size = 5u;
00497       break;
00498     }
00499     case ('E'):
00500     {
00501       /* Cap test */
00502       break;
00503     }
00504     case ('G'):
00505     {
00506       /* Cap test */
00507       break;
00508     }
00509     case ('I'):
00510     {
00511       /* Cap test */
00512       break;
00513     }
00514       
00515     default:
00516     {
00517       /* Fail whatever test we were requested */
00518       *pu8RawResponse = CLEAR;
00519       *pu8Size = (uint8_t)1u;
00520       vfnCopyArray((uint8_t*)&cau8GUIStat01, pu8TextResponse, \
00521                                 N_ELEMENTS(cau8GUIStat01));
00522       *pu8TextSize = N_ELEMENTS(cau8GUIStat01);
00523       break;
00524     }
00525   }/* End switch/case */
00526   
00527   return (u32Status);
00528 }
00529 /*
00530  ******************************************************************************
00531  * u32fnGUISendResponseFrame
00532  ******************************************************************************
00533  */
00534 uint32_t u32fnGUISendResponseFrame(const uint8_t* pu8Cmd,
00535                                    const uint8_t* pu8RawResponse, 
00536                                    const uint8_t* pu8Size,
00537                                    const uint8_t* pu8TextResponse,
00538                                    const uint8_t  u8TextSize)
00539 {
00540   uint32_t u32LocalStatus;
00541   uint32_t u32LocalTime;
00542   uint8_t  au8MinTime[2u];
00543   uint8_t  au8ASCIIArray[50];
00544   
00545   u32LocalStatus = CLEAR;
00546   
00547   /* Construct response array */
00548   /* Transfer Command and arg info to the data array */
00549   au8ASCIIArray[LV_RESPONSE_CMD] = *pu8Cmd;
00550   au8ASCIIArray[LV_RESPONSE_ARG1] = *(pu8Cmd + 1u);
00551   au8ASCIIArray[LV_RESPONSE_ARG2] = *(pu8Cmd + 2u);
00552   au8ASCIIArray[LV_RESPONSE_ARG3] = *(pu8Cmd + 3u);
00553   
00554   /* Figure out what the time is, and store the */
00555   /* last two bytes in the data array */
00556   SCHED_WHAT_IS_THE_TIME(u32LocalTime);
00557   au8MinTime[CLEAR] = (uint8_t)(u32LocalTime >> BITS_IN_24);
00558   au8MinTime[TRUE]  = (uint8_t)(u32LocalTime >> BITS_IN_16);
00559   vfnHexToASCII((uint8_t*)au8MinTime, 
00560                 (uint8_t*)&au8ASCIIArray[LV_RESPONSE_TIME],
00561                 N_ELEMENTS(au8MinTime));
00562  
00563   
00564   /* Do the same for the actual data */
00565   vfnHexToASCII((uint8_t*)(pu8RawResponse), 
00566                 (uint8_t*)&(au8ASCIIArray[LV_RESPONSE_PASS]),
00567                 (uint16_t)(*pu8Size));
00568   
00569   /* Allow any pending message to finish */
00570   (void)u8fnLVWaitForTxPort();
00571   
00572   /* Now construct and send our message */
00573   u32LocalStatus = u8fnLabViewSendMSorMP((uint8_t*)au8ASCIIArray, 
00574                                          ((*pu8Size * 2u) + LV_RESPONSE_ADDER),
00575                                          LV_SEND_SERIALLY, LV_TI_MSG);
00576   /* Allow any pending message to finish */
00577     (void)u8fnLVWaitForTxPort();
00578   u32LocalStatus = u8fnLabViewSendMSorMP((uint8_t*)pu8TextResponse, 
00579                                            u8TextSize,
00580                                            LV_SEND_SERIALLY, LV_TS_MSG);
00581 //#ifdef DEBUGGING_NOW
00582 //  DELAY_SEC(3u);
00583 //#endif
00584   return(u32LocalStatus);
00585 }
00586 /*
00587  ******************************************************************************
00588  * vfnGUISBCCapTests
00589  ******************************************************************************
00590  */
00591 void vfnGUISBCCapTests(uint8_t* pu8Size, uint8_t* pu8RawResponse,
00592                        uint8_t u8ERx, uint8_t u8Test)
00593 {
00594   /* locals */ 
00595   uint8_t u8Counter;
00596   uint32_t u32Status;
00597   
00598   u32Status = CLEAR;
00599   
00600   if(u8ERx < 0x06u)
00601   {
00602     u32Status = u32fnSBCPerformInitialTestsPOM(u8ERx, u8Test);
00603   }
00604   else
00605   {
00606     for(u8Counter = CLEAR; u8Counter < 6u; u8Counter++)
00607     {
00608       u32Status |= u32fnSBCPerformInitialTestsPOM(u8Counter, u8Test);
00609     }
00610   }
00611   
00612   /* Pass or fail criteria for this test is simple - if the Status */
00613   /* word is clear, we have passed, otherwise, we've failed. */
00614   *pu8RawResponse = (CLEAR == u32Status);
00615   /* We will send the status byte as part of the response. */
00616   *(pu8RawResponse + 1u) = (uint8_t)(u32Status >> BITS_IN_24);
00617   *(pu8RawResponse + 2u) = (uint8_t)(u32Status >> BITS_IN_16);
00618   *(pu8RawResponse + 3u) = (uint8_t)(u32Status >> BITS_IN_BYTE);
00619   *(pu8RawResponse + 4u) = (uint8_t)u32Status;
00620   *pu8Size = 5u;
00621   
00622   return;
00623 }
00624 /*
00625  ******************************************************************************
00626  * vfnGUISBCLeakageTests
00627  ******************************************************************************
00628  */
00629 void vfnGUISBCLeakageTests(const uint8_t cu8Side,
00630                            const uint8_t cu8Squib, 
00631                            const uint8_t cu8LeakageType, 
00632                            uint8_t* pu8Result)
00633 {
00634   uint32_t u32Status;
00635   uint8_t  u8Test;
00636   uint16_t u16ADCReading;
00637   
00638   u32Status = CLEAR;
00639   u8Test = CLEAR;
00640   
00641   /* Select either low-side or high-side elements */
00642   if(TRUE == cu8Side)
00643   {
00644     u8Test = cau8SBCInitialTestAoutRoutesIGL[cu8Squib];
00645   }
00646   else if(CLEAR == cu8Side)
00647   {
00648     u8Test = cau8SBCInitialTestAoutRoutesIGH[cu8Squib];
00649   }
00650   else
00651   {
00652     u8Test = CLEAR;
00653   }
00654   
00655   /* The test is performed here */
00656   /* Route the correct voltage value IGHx to AOUT */
00657   /* and Perform high leakage diagnosis */
00658   u32Status = u32fnSBCFLMLeakageMeasP1(u8Test, cu8LeakageType);
00659   DELAY_MSEC(2u);
00660   u32Status |= u32fnSBCFLMLeakageMeasP2((uint8_t)cu8LeakageType, 
00661                                         u8Test, 
00662                                         (uint16_t*)&u16ADCReading);
00663   DELAY_MSEC(2u);
00664   
00665   /* Store result */
00666   *pu8Result = (uint8_t)(CLEAR == u32Status);
00667   *(pu8Result + 1u) = (uint8_t)(u32Status >> BITS_IN_24);
00668   *(pu8Result + 2u) = (uint8_t)(u32Status >> BITS_IN_16);
00669   *(pu8Result + 3u) = (uint8_t)(u32Status >> BITS_IN_BYTE);
00670   *(pu8Result + 4u) = (uint8_t)u32Status;
00671   *(pu8Result + 5u) = (uint8_t)(u16ADCReading >> BITS_IN_BYTE);
00672   *(pu8Result + 6u) = (uint8_t)(u16ADCReading);
00673   
00674   return;
00675 }
00676 /*
00677  ******************************************************************************
00678  * vfnVerifyValidityOfCmd
00679  ******************************************************************************
00680  */
00681 void vfnGUICheckSatellites(const uint16_t* pu16Mask, 
00682                            uint8_t* pu8Result)
00683 {
00684   uint32_t au32LocalSchedResults[128];
00685   uint16_t  u16Mask;
00686   uint8_t   u8Counter;
00687   uint16_t u16Status;
00688   
00689   u8Counter = CLEAR;
00690   u16Status = CLEAR;
00691   u16Mask = BIT0;
00692   
00693   /* In order to read back satellites, we must: */
00694   /* 1) Turn the CG147 into Programming mode */
00695   /* 2) Turn power supplies off */
00696   /* 3) Wait    */
00697   /* 4) Turn on power supplies */
00698   /* 5) Gather data */
00699   /* 6) Reconfigure CG147 as before and exit */
00700   
00701   /* 1, 2, 3, & 4: reset the device to unlock EOP */
00702   (void)u8fnLaunchScheduler(CLEAR);
00703   vfnSBCPreSchedulerInit();
00704   (void)u8fnLaunchScheduler(TRUE);
00705   
00706   /* Transfer initial settings back to device, including WD */
00707   (void)u8fnCG147ScheduleBatchTransfer(CG147_SPI_CONFIG,
00708                                  (uint8_t*)&cau8CG147InitCmdsIn16Bit,
00709                                  (uint8_t*)&cau8CG147InitArgsIn16Bit,
00710                                  (uint16_t*)&au32LocalSchedResults,
00711                                  cu8CG147InitCmdsArgsSize,
00712                                  (uint32_t*)&gu32SBCTime);
00713   (void)u8fnCG147ScheduleSafeTransfer(CG147_SPI_CONFIG_EXT,
00714                                       SBC_WD2_TRIGGER,
00715                                       SBC_WD_RESPONSE_8,
00716                                       (uint16_t*)&gu32CG147WatchDog2RequestToMCU,
00717                                       (uint32_t*)&gu32SBCTime);
00718   (void)u8fnCG147ScheduleSafeTransfer(CG147_SPI_CONFIG_EXT,
00719                                       SBC_WD3_TRIGGER,
00720                                       SBC_WD_RESPONSE_8,
00721                                       (uint16_t*)&gu32CG147WatchDog3RequestToMCU,
00722                                       (uint32_t*)&gu32SBCTime);
00723   /* Here we wait for the scheduler to send the above SPI frames */
00724   while(1u > u32fnSchedIsTxDone(gu32SBCTime))
00725   {
00726     /* Wait here */
00727   };
00728   
00729   /* 5): Here we gather all the data and store it in RAM */
00730   u16Status = u16fnSBCGatherPSI5SatelliteData((uint8_t*)&gu8SBCSatelliteData);
00731   
00732   /*6)  We will now reset and restart */
00733   (void)u8fnLaunchScheduler(CLEAR);
00734   vfnSBCPreSchedulerInit();
00735   (void)u8fnLaunchScheduler(TRUE);
00736   (void)u32fnSBCInit();
00737   (void)u8fnSBCSchedulePSync();
00738   DELAY_MSEC(1u);
00739   
00740   /* Next we will verify that all sensors are happy */
00741   *pu8Result = (uint8_t)(u16Status >> 15u);
00742   
00743   for(u8Counter = 1u; u8Counter <= 12u; u8Counter++)
00744   {
00745     u16Mask <<= 1u;
00746     if(u16Mask & *pu16Mask)
00747     {
00748       *(pu8Result + u8Counter) = gu8SBCSatelliteData[DEVCFG1 * u8Counter];
00749       
00750     }
00751     else
00752     {
00753       /* Nothing */
00754     }
00755   }
00756   
00757   return;
00758 }
00759 /*
00760  ******************************************************************************
00761  * vfnVerifyValidityOfCmd
00762  ******************************************************************************
00763  */
00764 void vfnVerifyValidityOfCmd(const uint8_t* pu8Cmd, 
00765                             const GUICmdString_t* ptValidCmd,
00766                             const uint16_t u16TableSize,
00767                             uint8_t* pu8Result)
00768 {
00769   uint16_t u16Counter1;
00770   
00771   *pu8Result = CLEAR;
00772   
00773   /* We will verify all instances of the table */
00774   for(u16Counter1 = CLEAR; u16Counter1 < u16TableSize; u16Counter1++)
00775   {
00776     /* If the command is in the table, we will procede with more verification */
00777     if(*(pu8Cmd + 1u) == ptValidCmd[u16Counter1].A[CLEAR])
00778     {
00779       if((*(pu8Cmd + 2u) == ptValidCmd[u16Counter1].A[1u]) ||
00780            (ptValidCmd[u16Counter1].A[1u] == '*')) 
00781       {
00782         if((*(pu8Cmd + 3u) == ptValidCmd[u16Counter1].A[2u]) ||
00783            (ptValidCmd[u16Counter1].A[2u] == '*')) 
00784         {              
00785           /* Command is valid
00786           *pu8Result = TRUE;
00787           break;
00788         }
00789         else
00790         {
00791           /* Nothing */
00792         }
00793       }
00794       else
00795       {
00796         /* Nothing */
00797       }
00798     }
00799     else
00800     {
00801       /* Loop */
00802     }
00803   }
00804   
00805   return;
00806 }
00807 /*
00808  ******************************************************************************
00809  * vfnVerifyValidityOfCmd
00810  ******************************************************************************
00811  */
00812 void vfnGUISatAndSquibState(const uint8_t u8Mode, const uint16_t cu16Step)
00813 {
00814   static uint8_t au8Result[256];
00815   static uint16_t u16Token;
00816   uint16_t u16Counter;
00817   
00818   u16Counter = CLEAR;
00819   
00820   /* We will construct an ASCII message with a list of active satellites */
00821   /* and squibs. au8Result will be our place-holder. */
00822   
00823   /* Broken down into steps because of time constrains */
00824   switch(cu16Step)
00825   {
00826     /* First we do the operation for squibs */
00827     case(0u):
00828     {
00829       u16Token = N_ELEMENTS(cau8GUIStat03);
00830       break;
00831     }
00832     case(1u):
00833     {
00834       vfnCopyArray((uint8_t*)cau8GUIStat03, 
00835                    (uint8_t*)au8Result, 
00836                    u16Token);
00837       break;
00838     }
00839     case(2u):
00840     {
00841       for(u16Counter = BIT0; u16Counter < BIT5; u16Counter <<= BIT0)
00842       {
00843         vfnHexToASCIIMaskToList(gu16ActiveSquibChannels,
00844                                 (uint8_t*)au8Result,
00845                                 (uint16_t*)&u16Token,
00846                                 u16Counter);
00847       }
00848       break;
00849     }
00850     case(3u):
00851     {
00852       for(u16Counter = BIT5; u16Counter < BIT9; u16Counter <<= BIT0)
00853       {
00854         vfnHexToASCIIMaskToList(gu16ActiveSquibChannels,
00855                                 (uint8_t*)au8Result,
00856                                 (uint16_t*)&u16Token,
00857                                 u16Counter);
00858       }
00859       break;
00860     }
00861     case(4u):
00862     {
00863       for(u16Counter = BIT9; u16Counter < BIT12; u16Counter <<= BIT0)
00864       {
00865         vfnHexToASCIIMaskToList(gu16ActiveSquibChannels,
00866                                 (uint8_t*)au8Result,
00867                                 (uint16_t*)&u16Token,
00868                                 u16Counter);
00869       }
00870       break;
00871     }
00872     case(5u):
00873     {
00874       u16Token -= 2u;
00875       au8Result[u16Token++] = '.';
00876       au8Result[u16Token++] = ' ';
00877     }
00878     case(6u):
00879     {
00880       /* We then do the same thing for satellites */
00881       vfnCopyArray((uint8_t*)cau8GUIStat04, 
00882                    (uint8_t*)&au8Result[u16Token], 
00883                    N_ELEMENTS(cau8GUIStat04));
00884       break;
00885     }
00886     case(7u):
00887     {
00888       u16Token += N_ELEMENTS(cau8GUIStat04);
00889       break;
00890     }
00891     case(8u):
00892     {
00893       for(u16Counter = BIT0; u16Counter < BIT5; u16Counter <<= BIT0)
00894       {
00895         vfnHexToASCIIMaskToList(gu16ActivePSI5Channels,
00896                                 (uint8_t*)au8Result,
00897                                 (uint16_t*)&u16Token,
00898                                 u16Counter);
00899       }
00900       break;
00901     }
00902     case(9u):
00903     {
00904       for(u16Counter = BIT5; u16Counter < BIT9; u16Counter <<= BIT0)
00905       {
00906         vfnHexToASCIIMaskToList(gu16ActivePSI5Channels,
00907                                 (uint8_t*)au8Result,
00908                                 (uint16_t*)&u16Token,
00909                                 u16Counter);
00910       }
00911       break;
00912     }
00913     case(10u):
00914     {
00915       for(u16Counter = BIT9; u16Counter < BIT12; u16Counter <<= BIT0)
00916       {
00917         vfnHexToASCIIMaskToList(gu16ActivePSI5Channels,
00918                                 (uint8_t*)au8Result,
00919                                 (uint16_t*)&u16Token,
00920                                 u16Counter);
00921       }
00922       break;
00923     }
00924     case(11u):
00925     {
00926       u16Token -= 2u;
00927       au8Result[u16Token++] = '.';
00928       /* MS/MP won't take any message that is greater than 255 characters */
00929       if(u16Token > UCHAR_MAX)
00930       {
00931         u16Token = UCHAR_MAX;
00932       }
00933       else
00934       {
00935         /* Let it be */
00936       }
00937       break;
00938     }
00939     case(12u):
00940     {
00941       /* And we send out our data */
00942       (void)u8fnLabViewSendMSorMP((uint8_t*)au8Result, 
00943                                   (uint8_t)u16Token, 
00944                                   u8Mode,
00945                                   LV_MS_MSG);
00946       break;
00947     }
00948     default:
00949     {
00950       /* Leave */
00951     }
00952   }
00953   return;
00954 }
00955 /*
00956  ******************************************************************************
00957  *
00958  *  End of file.
00959  *
00960  ******************************************************************************
00961  */